﻿/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：曾永佑
 * 版本：
 * 创建日期：2013-02-20
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */  
        
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SRF.Components.DataDictionary
{
    /// <summary>
    /// 数据字典 操作函数 
    /// 第一次调用GetDictionaryItems和GetDictionaryText时会把字典加载到缓存
    /// 如果修改字典在数据库的数据可调用Refresh刷新缓存
    /// </summary>
    public class DataDictionaryHelper
    {
        /// <summary>
        /// 数据字典 key
        /// </summary>
        static string CacheKey = "DictionaryTable";
        /// <summary>
        /// 获取所有字典数据的sqlkey
        /// </summary>
        static string DictionarySQL = "admin.dictionary.all";

        #region

        /// <summary>
        /// 获取字典列表
        /// </summary>
        /// <param name="dictCode">字典code</param>
        /// <returns>字典列表</returns>
        public static IEnumerable<KeyValuePair<string, string>> GetDictionaryItems(string dictCode)
        {
            System.Data.DataView dictView = GetDictView();
            Dictionary<string, string> items = new Dictionary<string, string>();
            dictView.RowFilter = string.Format("DictCode='{0}'", dictCode);
            foreach (System.Data.DataRow row in dictView.Table.Rows)
            {
                items.Add(row["Code"].ToString(), row["Text"].ToString());
            }

            return items;
        }
        /// <summary>
        /// 根据字典项code获取字典项名称
        /// </summary>
        /// <param name="dictItemCode">字典项code</param>
        /// <returns></returns>
        public static string GetDictionaryText(string dictItemCode)
        {
            System.Data.DataView dictView = GetDictView();
            dictView.RowFilter = string.Format("Code='{0}'", dictItemCode);
            if (dictView.Table.Rows.Count > 0)
                return dictView.Table.Rows[0]["Text"].ToString();
            else
                return "";
        }
        /// <summary>
        /// 清除字典缓存
        /// 下次访问字典时从数据库重新
        /// </summary>
        public static void Refresh()
        {
            System.Web.HttpRuntime.Cache.Remove(CacheKey);
        }

        #endregion

        private static System.Data.DataView GetDictView()
        {
            System.Data.DataView dictView;
            if (System.Web.HttpRuntime.Cache.Get(CacheKey) == null)
            {
                Data.DBHelperBase dbhelper = Data.DBHelperFactory.GetDBHelper();
                dictView = new System.Data.DataView(dbhelper.GetQuery(DictionarySQL).DataSet().Tables[0]);
                System.Web.HttpRuntime.Cache.Add(CacheKey, dictView, null,
                    System.Web.Caching.Cache.NoAbsoluteExpiration,
                    TimeSpan.Zero,
                    System.Web.Caching.CacheItemPriority.High,
                    null);
            }
            else
            {
                dictView = System.Web.HttpRuntime.Cache.Get(CacheKey) as System.Data.DataView;
            }

            return dictView;
        }
    }
}